package com.qianfeng.openplatform.webmaster.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qianfeng.openplatform.commons.field.RedisKeyPrefix;
import com.qianfeng.openplatform.commons.field.util.RabbitAdviceBean;
import com.qianfeng.openplatform.commons.field.util.RabbitTypeEnum;
import com.qianfeng.openplatform.webmaster.client.CacheClient;
import com.qianfeng.openplatform.webmaster.entry.LayuiDataTableSource;
import com.qianfeng.openplatform.webmaster.entry.OpenApiMaster;
import com.qianfeng.openplatform.webmaster.mapper.ApiMappingMapper;
import com.qianfeng.openplatform.webmaster.pojo.ApiMapping;
import com.qianfeng.openplatform.webmaster.pojo.ApiMappingExample;
import com.qianfeng.openplatform.webmaster.rabbit.AdvierRoutingChange;
import com.qianfeng.openplatform.webmaster.service.ApiMappingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author 江江
 * @Date 2020/7/23 15:36
 */
@Service
public class ApiMappingServiceImpl implements ApiMappingService {

    @Autowired
    private ApiMappingMapper apiMappingMapper;
    @Autowired
    private CacheClient cacheClient; //cloud的假的 连接redis的
    @Autowired
    private AdvierRoutingChange routingChange;//消息队rabbitMQ



    @Override
    public LayuiDataTableSource getApiMappingPage(Integer page, Integer limit, String gatewayApiName, Integer state) {
        //System.out.println("========="+gatewayApiName+"=========");

        PageHelper.startPage(page, limit);
        ApiMappingExample example = new ApiMappingExample();
        ApiMappingExample.Criteria c = example.createCriteria();
        if (!StringUtils.isEmpty(gatewayApiName)) {
            c.andGatewayapinameLike("%" + gatewayApiName + "%");
        }
        if (!StringUtils.isEmpty(state)) {
            c.andStateEqualTo(state.byteValue());
        }
        List<ApiMapping> apiMappingList = apiMappingMapper.selectByExample(example);

        PageInfo<ApiMapping> info = new PageInfo<>(apiMappingList);
        LayuiDataTableSource source = new LayuiDataTableSource();
        source.setCount(info.getTotal());
        source.setData(info.getList());

        return source;
    }

    @Override
    public OpenApiMaster updateApiMapping(ApiMapping apiMapping) {
        int code = apiMappingMapper.updateByPrimaryKeySelective(apiMapping);
        if (code > 0) {
            try {
                if (apiMapping.getState() == 1) {
                    cacheClient.hMset(RedisKeyPrefix.WEB_MASTER_API_MAPPING_HASH_PRE + apiMapping.getGatewayapiname(), apiMapping);
                    RabbitAdviceBean bean = new RabbitAdviceBean();//准备要更新的内容
                    bean.setMessage(RedisKeyPrefix.WEB_MASTER_API_MAPPING_HASH_PRE+apiMapping.getGatewayapiname());
                    bean.setType(RabbitTypeEnum.UPDATE);
                    routingChange.output().send(MessageBuilder.withPayload(bean).build());
                } else {
                    cacheClient.deleteKey(RedisKeyPrefix.WEB_MASTER_API_MAPPING_HASH_PRE + apiMapping.getGatewayapiname());
                    RabbitAdviceBean bean = new RabbitAdviceBean();//准备要更新的内容
                    bean.setMessage(RedisKeyPrefix.WEB_MASTER_API_MAPPING_HASH_PRE+apiMapping.getGatewayapiname());
                    bean.setType(RabbitTypeEnum.UPDATE);
                    routingChange.output().send(MessageBuilder.withPayload(bean).build());
                }
            } catch (Exception e) {
                e.printStackTrace();

        }
        return OpenApiMaster.ok();
    }
        return OpenApiMaster.error("更改失败");
}


@Override
public OpenApiMaster addApiMapping(ApiMapping apiMapping){
        int code=apiMappingMapper.insert(apiMapping);
        if(code>0){
        try{
        cacheClient.hMset(RedisKeyPrefix.WEB_MASTER_API_MAPPING_HASH_PRE+apiMapping.getGatewayapiname(),apiMapping);
            RabbitAdviceBean bean = new RabbitAdviceBean();//准备要更新的内容
            bean.setMessage(RedisKeyPrefix.WEB_MASTER_API_MAPPING_HASH_PRE+apiMapping.getGatewayapiname());
            bean.setType(RabbitTypeEnum.UPDATE);
        routingChange.output().send(MessageBuilder.withPayload(bean).build());
        }catch(Exception e){
        e.printStackTrace();
        }
        return OpenApiMaster.ok();
        }
        return OpenApiMaster.error("添加失败");
        }

@Override
public OpenApiMaster delApiMapping(Integer[] ids){
    System.out.println(ids.toString());
    List<ApiMapping> mappings= apiMappingMapper.findApiMappingToIds(ids);
        int code=apiMappingMapper.deleteByPrimaryKeyAll(ids);

        if(code>0){
            try{
                for (ApiMapping apiMapping : mappings) {
                    cacheClient.deleteKey(RedisKeyPrefix.WEB_MASTER_API_MAPPING_HASH_PRE + apiMapping.getGatewayapiname());
                    RabbitAdviceBean bean = new RabbitAdviceBean();//准备要更新的内容
                    bean.setMessage(RedisKeyPrefix.WEB_MASTER_API_MAPPING_HASH_PRE+apiMapping.getGatewayapiname());
                    bean.setType(RabbitTypeEnum.UPDATE);
                    routingChange.output().send(MessageBuilder.withPayload(bean).build());
                }
            } catch (Exception e){
                e.printStackTrace();
            }
        return OpenApiMaster.ok();
        }
        return OpenApiMaster.error("删除失败");
        }


        }
